package com.zhiche.lisa.lspm.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.lspm.dao.model.sys.Permission;
import com.zhiche.lisa.lspm.service.sys.IPermissionService;
import com.zhiche.lisa.lspm.vo.driver.DriverCheckinLineVO;
import com.zhiche.lisa.lspm.vo.sys.ElTreeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限 接口
 *
 * @author hxh
 * @since 2018-09-28
 */
@RestController
@RequestMapping("/permission")
@Api(value = "/permission", description = "权限接口", tags = {"权限接口"})
public class PermissionController {

    private static final Logger LOGGER = LoggerFactory.getLogger(CarrierController.class);

    @Autowired
    private IPermissionService permissionService;

    @GetMapping(value = "/treeAllUsable")
    public RestfulResponse<List<ElTreeVO>> getSubMenu() {
        RestfulResponse<List<ElTreeVO>> responseBO = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        List<Permission> listAllPermission = permissionService.queryAllUsablePermission();
        ElTreeVO elTreeVO = new ElTreeVO();
        elTreeVO.setId(0);
        elTreeVO.setLabel("根权限");
        buildElTree(elTreeVO, listAllPermission);
        responseBO.setData(elTreeVO.getChildren());
        return responseBO;
    }

    private void buildElTree(ElTreeVO elTreeVO, List<Permission> listAllPermission) {
        List<ElTreeVO> children = new ArrayList<>();
        for (Permission permission : listAllPermission) {
            if (permission.getPid().equals(elTreeVO.getId())) {
                ElTreeVO childElTree = new ElTreeVO();
                childElTree.setId(permission.getOrders());
                childElTree.setLabel(permission.getName());
                buildElTree(childElTree, listAllPermission);
                children.add(childElTree);
            }

            elTreeVO.setChildren(children);
        }
    }

    @GetMapping(value = "/levelOne")
    @ApiOperation(value = "一级权限", notes = "一级权限", produces = MediaType
            .APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<Permission[]> levelOne(@RequestParam Integer roleId) {
        LOGGER.info("PermissionController.levelOne（一级权限） params : {} ", roleId);
        RestfulResponse<Permission[]> result = new RestfulResponse<>(CommonEnum
                .SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(permissionService.levelOne(roleId));
        } catch (BaseException e) {
            LOGGER.error("PermissionController.levelOne ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("PermissionController.levelOne ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(),
                    null);
        }
        return result;
    }

    @GetMapping(value = "/levelTwo")
    @ApiOperation(value = "二级权限", notes = "二级权限", produces = MediaType
            .APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<Permission[]> levelTwo(@RequestParam Integer pid,@RequestParam Integer roleId) {
        LOGGER.info("PermissionController.levelOne（二级权限） params : {} ", pid);
        RestfulResponse<Permission[]> result = new RestfulResponse<>(CommonEnum
                .SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(permissionService.levelTwo(roleId,pid));
        } catch (BaseException e) {
            LOGGER.error("PermissionController.levelTwo ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("PermissionController.levelTwo ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(),
                    null);
        }
        return result;
    }

}

